package cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit.member;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit.member.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.customer.serviceUnit.member.ServiceUnitMemberDO;
import cn.iocoder.yudao.module.biz.service.customer.serviceUnit.member.ServiceUnitMemberService;

@Tag(name = "管理后台 - 客户档案-服务单元-组员")
@RestController
@RequestMapping("/san/biz-customer-archive-service-unit-member")
@Validated
public class ServiceUnitMemberController {

    @Resource
    private ServiceUnitMemberService serviceUnitMemberService;

    @PostMapping("/create")
    @Operation(summary = "创建客户档案-服务单元-组员")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-member:create')")
    public CommonResult<Long> createServiceUnitMember(@Valid @RequestBody ServiceUnitMemberSaveReqVO createReqVO) {
        return success(serviceUnitMemberService.createServiceUnitMember(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户档案-服务单元-组员")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-member:update')")
    public CommonResult<Boolean> updateServiceUnitMember(@Valid @RequestBody ServiceUnitMemberSaveReqVO updateReqVO) {
        serviceUnitMemberService.updateServiceUnitMember(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户档案-服务单元-组员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-member:delete')")
    public CommonResult<Boolean> deleteServiceUnitMember(@RequestParam("id") Long id) {
        serviceUnitMemberService.deleteServiceUnitMember(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客户档案-服务单元-组员")
                @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-member:delete')")
    public CommonResult<Boolean> deleteServiceUnitMemberList(@RequestParam("ids") List<Long> ids) {
        serviceUnitMemberService.deleteServiceUnitMemberListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户档案-服务单元-组员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-member:query')")
    public CommonResult<ServiceUnitMemberRespVO> getServiceUnitMember(@RequestParam("id") Long id) {
        ServiceUnitMemberDO serviceUnitMember = serviceUnitMemberService.getServiceUnitMember(id);
        return success(BeanUtils.toBean(serviceUnitMember, ServiceUnitMemberRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户档案-服务单元-组员分页")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-member:query')")
    public CommonResult<PageResult<ServiceUnitMemberRespVO>> getServiceUnitMemberPage(@Valid ServiceUnitMemberPageReqVO pageReqVO) {
        PageResult<ServiceUnitMemberDO> pageResult = serviceUnitMemberService.getServiceUnitMemberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceUnitMemberRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户档案-服务单元-组员 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-member:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceUnitMemberExcel(@Valid ServiceUnitMemberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceUnitMemberDO> list = serviceUnitMemberService.getServiceUnitMemberPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户档案-服务单元-组员.xls", "数据", ServiceUnitMemberRespVO.class,
                        BeanUtils.toBean(list, ServiceUnitMemberRespVO.class));
    }

}